package com.kernel.kotlintest

import io.gitee.zhangbinhub.acp.core.common.security.*
import io.gitee.zhangbinhub.acp.core.common.security.key.KeyManagement
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import java.security.interfaces.DSAPrivateKey
import java.security.interfaces.DSAPublicKey
import java.security.interfaces.RSAPrivateKey
import java.security.interfaces.RSAPublicKey

class TestEncrypt {
    @Test
    fun encrypt() {
        val plainText = "你好：hello world"
        val hmacKeyStr = "test_key"
        println("--------------------------------------------------------------------------")
        println("plainText: $plainText")
        println("hmacKeyStr: $hmacKeyStr")
        println("  HMAC: ${HmacEncrypt.encrypt(plainText, KeyManagement.getKey(hmacKeyStr, HmacEncrypt.CRYPT_TYPE))}")
        println("   MD5: ${Md5Encrypt.encrypt(plainText)}")
        println("  SHA1: ${Sha1Encrypt.encrypt(plainText)}")
        println("SHA256: ${Sha256Encrypt.encrypt(plainText)}")
        println("   SM3: ${SM3Encrypt.encrypt(plainText)}")
        println("--------------------------------------------------------------------------")
        // aes
        val aesKeyStr = "test_key_1234567"
        println("plainText: $plainText")
        println("aesKeyStr: $aesKeyStr")
        val aesKey = KeyManagement.getAESKey(aesKeyStr)
        val aesEncryptedText = AesEncrypt.encrypt(plainText, aesKey)
        val aesDecryptedText = AesEncrypt.decrypt(aesEncryptedText, aesKey)
        println("AES encrypt: $aesEncryptedText")
        println("AES decrypt: $aesDecryptedText")
        Assertions.assertEquals(aesDecryptedText, plainText)
        println("--------------------------------------------------------------------------")
        // des
        val desKeyStr = "test_key_123456789length"
        println("plainText: $plainText")
        println("desKeyStr: $desKeyStr")
        var desKey = KeyManagement.get3DESKey(desKeyStr)
        var desEncryptedText = DesEncrypt.encryptBy3DesEcb(plainText, desKey)
        var desDecryptedText = DesEncrypt.decryptBy3DesEcb(desEncryptedText, desKey)
        println("3DES encrypt: $desEncryptedText")
        println("3DES decrypt: $desDecryptedText")
        Assertions.assertEquals(desDecryptedText, plainText)
        println("desKeyStr: ${desKeyStr.substring(0, 8)}")
        desKey = KeyManagement.getDESKey(desKeyStr.substring(0, 8))
        desEncryptedText = DesEncrypt.encryptByDesEcb(plainText, desKey)
        desDecryptedText = DesEncrypt.decryptByDesEcb(desEncryptedText, desKey)
        println("DES encrypt: $desEncryptedText")
        println("DES decrypt: $desDecryptedText")
        Assertions.assertEquals(desDecryptedText, plainText)
        println("--------------------------------------------------------------------------")
        // dsa
        println("plainText: $plainText")
        val dsaKeys = KeyManagement.getDsaKeys()
        val dsaPuk = dsaKeys[0] as DSAPublicKey
        val dsaPrk = dsaKeys[1] as DSAPrivateKey
        val dsaSign = DsaEncrypt.sign(plainText, dsaPrk)
        val dsaVerify = DsaEncrypt.verify(plainText, dsaPuk, dsaSign)
        println("DSA sign: $dsaSign")
        println("DSA verify: $dsaVerify")
        Assertions.assertEquals(dsaVerify, true)
        println("--------------------------------------------------------------------------")
        // rsa
        println("plainText: $plainText")
        val rsaKeys = KeyManagement.getRsaKeys()
        val rsaPuk = rsaKeys[0] as RSAPublicKey
        val rsaPrk = rsaKeys[1] as RSAPrivateKey
        var rsaEncryptedText = RsaEncrypt.encryptByPublicKey(plainText, rsaPuk)
        var rsaDecryptedText = RsaEncrypt.decryptByPrivateKey(rsaEncryptedText, rsaPrk)
        println("RSA public key encrypt: $rsaEncryptedText")
        println("RSA private key decrypt: $rsaDecryptedText")
        Assertions.assertEquals(rsaDecryptedText, plainText)
        rsaEncryptedText = RsaEncrypt.encryptByPrivateKey(plainText, rsaPrk)
        rsaDecryptedText = RsaEncrypt.decryptByPublicKey(rsaEncryptedText, rsaPuk)
        println("RSA private key encrypt: $rsaEncryptedText")
        println("RSA public key decrypt: ${RsaEncrypt.decryptByPublicKey(rsaEncryptedText, rsaPuk)}")
        Assertions.assertEquals(rsaDecryptedText, plainText)
        println("--------------------------------------------------------------------------")
        // sm2
        println("plainText: $plainText")
        val sm2Keys = KeyManagement.getSM2Keys()
        val sm2Puk = sm2Keys[0]
        val sm2Prk = sm2Keys[1]
        val sm2EncryptedText = SM2Encrypt.encrypt(plainText, sm2Puk)
        val sm2DecryptedText = SM2Encrypt.decrypt(sm2EncryptedText, sm2Prk)
        println("SM2 encrypt: $sm2EncryptedText")
        println("SM2 decrypt: $sm2DecryptedText")
        Assertions.assertEquals(sm2DecryptedText, plainText)
        val sm2Sign = SM2Encrypt.sign(plainText, sm2Prk)
        val sm2Verify = SM2Encrypt.verify(plainText, sm2Puk, sm2Sign)
        println("SM2 sign: $sm2Sign")
        println("SM2 verify: $sm2Verify")
        Assertions.assertEquals(sm2Verify, true)
        println("--------------------------------------------------------------------------")
        val sm4KeyStr = "test_key_1234567"
        println("plainText: $plainText")
        println("sm4KeyStr: $sm4KeyStr")
        val sm4Key = KeyManagement.getSM4Key(sm4KeyStr)
        val sm4EncryptedText = SM4Encrypt.encrypt(plainText, sm4Key)
        val sm4DecryptedText = SM4Encrypt.decrypt(sm4EncryptedText, sm4Key)
        println("SM4 encrypt: $sm4EncryptedText")
        println("SM4 decrypt: $sm4DecryptedText")
        Assertions.assertEquals(sm4DecryptedText, plainText)
    }
}